package com.sinopec.android.service;


import com.sinopec.android.HomeActivity;
import com.sinopec.android.LoginActivity;
import com.sinopec.android.R;
import com.sinopec.android.util.LogUtil;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;
import android.widget.RemoteViews;

/**
* @ClassName: NotificationService
* @Description: TODO(后台service)
* @author liuwei
* @date 2012-6-5 上午10:41:06
* 
*/
public class NotificationService extends Service {
	private Context mContext = this;
    private NotificationManager mNotificationManager;  
    PendingIntent m_PendingIntent;  

     //声明Notification对象  

     Notification  m_Notification; 

    final int pullDelay = 60000;//检查配置更新服务的延迟时间间隔   单位ms  1分钟间隔
	 Handler handlerZoom = new Handler(){
	    	public void handleMessage(android.os.Message msg) {

	    	}
	    };
	 Handler handler = new Handler(){
	    	public void handleMessage(android.os.Message msg) {
	    		switch (msg.what) {
				case 1:
					showNotification();
					
					break;

				default:
					break;
				}
	    	}
	    };   
	    Runnable pullChecker = new Runnable()
	    {
	        public void run()
	        {
	        	LogUtil.printInfo("NotificationService运行了.....");
	        	//1、调用pull接口
	        	//2、将返回结果，以通知形式展示
	        		handler.sendEmptyMessage(1);
	        	//3、点击通知，进入程序首页
	        	handlerZoom.removeCallbacks(pullChecker);
	            handlerZoom.postDelayed(pullChecker, pullDelay); // register a new one
	        }
	    };
	@Override
	public void onCreate() {
		LogUtil.printInfo("启动pull消息服务");
		super.onCreate();
	}

	@Override
	public void onDestroy() {
		LogUtil.printInfo("销毁pull消息服务");
		super.onDestroy();
	}

	@Override
	public void onStart(Intent intent, int startId) {
	    handlerZoom.postDelayed(pullChecker, pullDelay);
		mNotificationManager = (NotificationManager) getSystemService(android.content.Context.NOTIFICATION_SERVICE);
		super.onStart(intent, startId);
	}

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
	
	 /** 设置 */ 

	  private void showNotification(){   

	   //点击通知时转移内容   

	  Intent intent = new Intent(this, LoginActivity.class);  

	   //intent.addCategory(WINDOW_SERVICE);  

	   //主要是设置点击通知时显示内容的类   

	   m_PendingIntent = PendingIntent.getActivity(mContext, 0, intent, 0); //如果转移内容则用m_Intent();  

	   //构造Notification对象   

	   m_Notification = new Notification();   

	   //设置通知在状态栏显示的图标   

	   m_Notification.icon = R.drawable.ic_launcher;  

	   //当我们点击通知时显示的内容   

	   m_Notification.tickerText = "您有新消息来啦";   

	   //通知时发出默认的声音   

	   m_Notification.defaults = Notification.DEFAULT_SOUND;   
	   //点击自动取消
	   m_Notification.flags = Notification.FLAG_AUTO_CANCEL;


	   //设置通知显示的参数   

	   m_Notification.setLatestEventInfo(mContext, "通知", "您有新消息来啦，请注意查收！", m_PendingIntent);   

	   //可以理解为执行这个通知   

	  mNotificationManager.notify(0, m_Notification);   

	  }  

	  /** 取消 */ 

	  private void cancelNotification(){  

	   mNotificationManager.cancelAll();  

  } 


}
